package com.hjy.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.hjy.common.utils.PageUtils;
import com.hjy.common.utils.R;
import com.hjy.product.vo.AttrGroupAndAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hjy.product.entity.AttrGroupEntity;
import com.hjy.product.service.AttrGroupService;


/**
 * 属性分组
 *
 * @author hjy
 * @email 2402668109@qq.com
 * @date 2022-07-16 01:39:19
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    //@RequiresPermissions("hjy:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catId") Long catId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catId);

        return R.ok().put("page", page);
    }
    // /product/attrgroup/{catelogId}/withattr
    @RequestMapping("/{catelogId}/withattr")
    public R getAttrGroupAndAttrs(@PathVariable("catelogId") Long catId){
        List<AttrGroupAndAttrsVo> list = attrGroupService.getAttrGroupAndAttrs(catId);

        return R.ok().put("data",list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        // 获取 catelogPath , 也就是三级数据
        attrGroup.setCatelogPath(attrGroupService.getThreeFloorCatId(attrGroup.getCatelogId()));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("hjy:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("hjy:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("hjy:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
